Advance Client Script In Service-now

Client Script:

Client Side Objects:

  • 1. G-FORM:GlideForm.js is the Javascript class used to customize forms.
    g_form.getValue(‘short_description’)
    g_form.setValue(‘short_description’, “”)
    g_form.addOption(‘priority’, ‘2.5’, ‘2.5 – Moderately High’, 3)
    g_form.getTableName()
    g_form.addErrorMessage(‘This is an error’)
    g_form.addInfoMessage(‘The top five fields in this form are mandatory’)
    g_form.showFieldMsg(‘impact’,’Low impact response time can be one week’,’info’)
    g_form.showFieldMsg(‘impact’,’Low impact not allowed with High priority’,’error’)
    g_form.flash(“incident.number”, “#FFFACD”, 0)

 

  • 2. G-User: g_user is a global object in GlideUser. g_user is a Client Script Object and contains name and role information about the current user.
    g_user.userName
    g_user.userID
    g_user.firstName
    g_user.getClientData(“loginlanguage”)
    g_user.hasRole(‘admin’)
    g_user.hasRoleExactly(‘util’)
    g_user.hasRoleFromList(“itil, maint”)
    g_user.hasRoles()


Client Side Examples:

.......................................................................................................................


1. Problem Statement: For New Record show alert.


function onLoad() {

if (g_form.isNewRecord()){

alert('This is new Record');

}

else{

alert('This is not a new record');

}

}

.......................................................................................................................

2. Problem Statement: To make "Convert to" field visible after creating record but not while creating Record


function onLoad() {

 

if(g_form.isNewRecord()){

g_form.setVisible("u_convert_to",false);

}

else {

g_form.setVisible("u_convert_to",true);

}

}


.......................................................................................................................

3. Problem Statement: When Assigned To value changes, populte mail id of changed user in email field and populate user name in short description


function onChange(control, oldValue, newValue, isLoading, isTemplate) {

if (isLoading || newValue === '') {

return;

}

var p=g_form.getReference("u_assigned_to").email;

g_form.setValue("u_email",p);

g_form.setMandatory("u_first_name",true);

g_form.setValue("u_short_description",'This is :'+g_user.getFullName());

}


.......................................................................................................................

4. Problem Statement: Confirm details before submit form

function onSubmit() {

//Type appropriate comment here, and begin script below

var a = g_form.getValue("u_first_name");

var b = g_form.getValue("u_last_name");

var con= confirm( 'You Entered below datails-\n The first name is- '+a+'\n The last name is- '+b+'\n Do you confirm the details?' );

if(con==true){

return;

}

else{

return 0;

}

}

.......................................................................................................................

5. Problem Statement: Hide the Clouser Information section from form while creating new record

function onLoad() {
//Type appropriate comment here, and begin script below
if(g_form.isNewRecord()){
g_form.setSectionDisplay("closure_information", false);
}else{
g_form.setSectionDisplay("closure_information", true)
}
}

.......................................................................................................................

6. Problem Statement: Remove New state from the choice if it is not New

function onLoad() {
//Type appropriate comment here, and begin script below
if(g_form.getValue("state")==1){
}else{
g_form.removeOption("state",'1');
}

}

.......................................................................................................................

7. Problem Statement: On change---Comments Make Work Notes Optional

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var requireOtherField = (newValue == '');
g_form.setMandatory('work_notes', requireOtherField);
}


.......................................................................................................................

8. Problem Statement: On Change- Work Notes Make Comments Optional

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var requireOtherField = (newValue == '') ;
g_form.setMandatory('comments', requireOtherField);
}


.......................................................................................................................

9. Problem Statement: If Caller is a VIP User then priority should be always P1.

On Change Client Script on Caller Field

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_form.getReference('caller_id', priorityCallback);

}

function priorityCallback(caller){


if (caller.vip == 'true') {
g_form.setValue("impact",1);
g_form.setValue("urgency",1);

}
}


.......................................................................................................................

10. Problem Statement: Start date should be less than End Date,if not show alert


Start date validation -
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var start_date = g_form.getValue("u_start_date");
var end_date = g_form.getValue("u_end_date");
var format = g_user_date_time_format;

if (start_date != "" && end_date != "") {
var isEndBeforeStart = compareDates(start_date, format, end_date, format);
//0 - means valid date(end date is more than start date)
//1 - means start is grater than end
if (isEndBeforeStart) {
alert("Start date can not be grater than end date");
g_form.setValue("u_start_date", " ");

}
}

}


.......................................................................................................................

11. Problem Statement: Expected Date Should be State date + 5 days

OnChange Client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var start_date = g_form.getValue("u_start_date");
var glideAjax = new GlideAjax("ExpectedCloseDateSI");
glideAjax.addParam("sysparm_name","getStartDate");
glideAjax.addParam("sysparm_start_date",start_date);
glideAjax.getXML(callBackFunction);

function callBackFunction(response){
var res = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue("u_expected_close_date",res);
}
//Type appropriate comment here, and begin script below

}


Script include
ExpectedCloseDateSI

var ExpectedCloseDateSI = Class.create();
ExpectedCloseDateSI.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getStartDate : function (){

var start_date_from_client = this.getParameter("sysparm_start_date");
var new_expected_date = new GlideDateTime(start_date_from_client);
new_expected_date.addDays(5);
return new_expected_date;
},
type: 'ExpectedCloseDateSI'
});

 

.......................................................................................................................

12. Problem Statement: Populate Email, Department, Contact, Company,location as per User value.

Client Script: On Change of User.

function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}

var name = g_form.getValue('user_name');
var ga = new GlideAjax('ClientSideUtils');
ga.addParam('sysparm_name', 'getUserDetails');
ga.addParam('sysparm_user', newValue);
ga.getXML(setUserDetails);


function setUserDetails(serverResponse) {
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
var split = answer.split('#');
g_form.setValue('email', split[0]);
g_form.setValue('department', split[1]);
g_form.setValue('user_contact', split[2]);
g_form.setValue('company',split[3]);
g_form.setValue('region',split[4]);
g_form.setValue('business_unit',split[5]);
}

}

 

Server Side Script:

var ClientSideUtils = Class.create();
ClientSideUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

getUserDetails: function() {
var usr = this.getParameter('sysparm_user');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', usr);
gr.query();
if (gr.next()) {
var email = gr.email;
var dept = gr.department;
var mob = gr.mobile_phone;
var com=gr.company;
var loc=gr.location;
var bunit=gr.location.getDisplayValue()+" Business Unit";
return email + "#" + dept + "#" + mob+"#"+com+"#"+loc+"#"+bunit;
}
},

 

type: 'ClientSideUtils'
});


.......................................................................................................................